//导入axios
import axios from "axios";

//实例化
var service = axios.create({
    timeout: 10 * 1000,
    baseURL: '/api',
})

//设置请求
service.interceptors.request.use((config) => {
    // config.headers[]
    config.headers['Authorization'] = 'Bearer' + ' ' + localStorage.getItem('token');
    //统一设置请求头
    return config;
}, (error) => {
    return Promise.reject(error);
});

//统一设置响应
service.interceptors.response.use((res) => {
    return res;
}, (error) => {
    if (error.response.status == 401) {
        alert('请求超时');
    } else if (error.response.status == 404) {
        alert('请求地址错误，未找到资源')
    } else if (error.response.status == 500) {
        alert('服务器出现错误,请联系管理员')
    }

    return Promise.reject(error);
})

//抛出实例化对象
export default service;